package com.datatest.riskcheck.tools;

import com.alibaba.fastjson.JSON;
import com.datatest.beans.CreditReportEntity;
import com.datatest.beans.ResultEntity;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

/**
 * @Author lxt
 * Created by Jccfc on 2021/9/28.
 */
public class CheckParam {

    private static String getVlue(Cell cell){
        if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){
            //返回布尔类型的值
            return String.valueOf(cell.getBooleanCellValue()).trim();
        } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
            //返回数值类型的值
            return String.valueOf(cell.getNumericCellValue()).trim();
        }else{
            //返回字符串类型的值
            return String.valueOf(cell.getStringCellValue()).trim();
        }
    }

    public static ResultEntity redExecl(InputStream is, String sheetName,Map<String,String> resultMap) throws IOException, InvalidFormatException {


        Workbook workbook = WorkbookFactory.create(is);
        List<Map<String,String>> list = new ArrayList<>();
        //Map<String, List> hashMap1 = new HashMap<>();
        //循环有哪些sheet工作表
        for (int numSheet = 0;numSheet < workbook.getNumberOfSheets();numSheet++){
            Sheet sheet = workbook.getSheetAt(numSheet);
            if (sheet == null){
                continue;
            }
            if (workbook.getSheetName(numSheet).trim().equals(sheetName.trim())){
                for (int rowNum = 0; rowNum < sheet.getPhysicalNumberOfRows();rowNum++){
                    Row row = sheet.getRow(rowNum);
                    if (row == null){
                        continue;
                    }
                    HashMap<String, String> hashMap = new HashMap<>();
                    for (int cellNum = 0;cellNum < row.getPhysicalNumberOfCells();cellNum++){
                        Cell cell = row.getCell(cellNum);
                        if (cell == null){
                            continue;
                        }
                        hashMap.put(cellNum+"",getVlue(cell));
                    }
                    list.add(hashMap);
                }
            }
        }
        Map<String, String> excelMap = new HashMap<>();
        Boolean flag = false;
        for (int i = 4;i < list.size();i++){
            for (Map.Entry<String,String> entry1 : list.get(i).entrySet()){
                if (entry1.getKey().equals("1")){
                    excelMap.put(entry1.getValue(),"");
                }
                if (entry1.getKey().equals("0") && entry1.getValue().equals("交易返回")){
                    flag = true;
                    break;
                }
            }
            if (flag == true){
                break;
            }
        }
        return checkData(excelMap,resultMap);
    }

    private static ResultEntity checkData(Map excelMap, Map<String,String> resultMap) {

        ResultEntity resultEntity = new ResultEntity();
        Iterator<Map.Entry<Integer, String>> it =  excelMap.entrySet().iterator();
        Map<String,Object> resultSuccess = new HashMap<>();
        List resultFail = new ArrayList<>();
        while(it.hasNext()){
            Map.Entry<Integer, String> entry = it.next();
            if(resultMap.get(entry.getKey()) == null){  //excel存在引擎返回不存在
                resultFail.add(entry.getKey());
            }else{
                resultSuccess.put(String.valueOf(entry.getKey()),entry.getValue());
            }
        }
        resultEntity.setResultFail(resultFail);
        resultEntity.setResultSuccess(resultSuccess);
        return resultEntity;
    }

    public static String PostApi(String seqId) throws IOException {
        URL url = new URL("http://10.10.100.124:9200/_search?q=_id:"+seqId);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestProperty("Connection","keep-alive");
        //设置请求方式
        connection.setRequestMethod("GET");
        //设置自己数组输出流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        connection.connect();//调用接口
        //获取接口返回接口
        InputStream inputStream = connection.getInputStream();
        try {
            byte[] bytes = new byte[1024];
            int read = 0;
            //将返回结果按自己读入read中
            while ((read=inputStream.read(bytes))!=-1){
                //讲读出字节数据写入数组输出流中
                byteArrayOutputStream.write(bytes,0,read);
            }
        }finally {
            assert inputStream !=null;
            inputStream.close();
            //接口连接断开
            connection.disconnect();
        }
        //将整数组调用toString方法
        String s = byteArrayOutputStream.toString();
        return s;
        //System.out.println(s);
        //     System.out.println(new String(byteArrayOutputStream.toByteArray()));//先转换为1个新字节的数组，再转换为String对象

    }

    public static void main(String[] args) throws IOException, InvalidFormatException {

        String fileName = "度小满风险引擎接口文档V1.2.xlsx";
        FileInputStream fileInputStream = new FileInputStream("D:\\" + fileName);
        //redExecl(fileInputStream, "贷前准入策略（异步）",)
        fileInputStream.close();
        String seqId = "1619658994541-59826170";
        PostApi(seqId);
    }
}
